package com.example.Blog.controller;

import com.example.Blog.model.Article;
import com.example.Blog.model.Comment;
import com.example.Blog.model.User;
import com.example.Blog.service.ArticleService;
import com.example.Blog.service.CommentService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController {
    final ArticleService articleService;
    final CommentService commentService;
    final int pageSize = 20;

    public ArticleController(ArticleService articleService, CommentService commentService) {
        this.articleService = articleService;
        this.commentService = commentService;
    }

    @GetMapping("/post")
    public String post(){
        return "article/compose";
    }

    // Post an article
    @PostMapping
    public String post(Article article, HttpServletRequest httpServletRequest) {
        User loginUser = (User) httpServletRequest.getSession().getAttribute("loginUser");
        if (loginUser != null) {
            article.setArticleBelongTo(loginUser.getUserId());
            article.setArticleTime(new Date());
            this.articleService.postArticle(article);
            return "redirect:/index";
        } else return "user/login";
    }

    @GetMapping
    public Article viewArticle(@RequestParam int articleId) {
        return this.articleService.findAnArticle(articleId);
    }

    @GetMapping("/deleteArticle")
    public String deleteArticle(@RequestParam int articleId, @RequestParam int userId) {
        // TODO: 2023/12/1 这里应该加入验证
        this.commentService.deleteByBelongToArticle(articleId);
        this.articleService.deleteArticle(articleId);
        return "redirect:/profile?pageIndex=0&userId=" + userId;
    }

    @GetMapping("/type")
    public String findAllByArticleType(@RequestParam String articleType, Model model,@RequestParam int pageIndex) {
        Page<Article> articleListPages = this.articleService.findAllArticlesInSameType(articleType, pageIndex, pageSize);
        int prePage = articleListPages.previousOrFirstPageable().getPageNumber();
        int nextPage = articleListPages.nextOrLastPageable().getPageNumber();
        model.addAttribute("type", articleType);
        model.addAttribute("prePage", prePage);
        model.addAttribute("nextPage", nextPage);
        model.addAttribute("articles", articleListPages);
        return "article/index";
    }

    @GetMapping("/detail")
    public String detail(Model model, @RequestParam int articleId){
        Article article = this.articleService.findAnArticle(articleId);
        List<Comment> comments = this.commentService.findAllByCommentBelongToArticleOrderByCommentLikes(articleId);
        model.addAttribute("article", article);
        model.addAttribute("comments", comments);
        return "/article/detail";
    }
}
